package com.javachmapi.springbootmongodb.controller;

import com.javachmapi.springbootmongodb.dao.BookDao;
import com.javachmapi.springbootmongodb.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * create by 宋利军
 */
@RestController
public class BookController {

    @Autowired
    BookDao bookDao;

    @Autowired
    MongoTemplate mongoTemplate;

    @GetMapping("/test")
    public void test(){
        List<Book> books = new ArrayList<>();

        Book book1 = new Book();
        book1.setId(1);
        book1.setName("《Java开发学习大纲文档》V7.x");
        book1.setAuthor("宋利军");
        books.add(book1);
        Book book2 = new Book();
        book2.setId(2);
        book2.setName("《Java开发学习大纲文档》V8.x");
        book2.setAuthor("宋利军");
        books.add(book2);
        bookDao.insert(books);
        List<Book> list = bookDao.findByAuthorContains("宋利军");
        for (Book book:list) {
            System.out.println(book.getName()+" | "+ book.getAuthor());
        }
        Book book = bookDao.findByNameEquals("《Java开发学习大纲文档》V8.x");
        System.out.println(book.toString());
    }

    @GetMapping("/test2")
    public void test2(){
        List<Book> books = new ArrayList<>();

        Book book1 = new Book();
        book1.setId(3);
        book1.setName("《红楼梦》");
        book1.setAuthor("曹雪芹");
        books.add(book1);
        Book book2 = new Book();
        book2.setId(4);
        book2.setName("《西游记》");
        book2.setAuthor("吴承恩");
        books.add(book2);
        mongoTemplate.insertAll(books);
        List<Book> list = mongoTemplate.findAll(Book.class);
        System.out.println(list);
        Book book = mongoTemplate.findById(3,Book.class);
        System.out.println(book);
    }
}
